/**
 * zmn角色模块-角色编辑
 * 文件命名格式：zmn.模块.表/功能.js
 * @author heguanghua
 * @since 2020/04/23 13:45
 */
layui.define(['form', 'layedit', 'zmnApp', 'zmnSelect', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;

    form.render(null, "zmn-form");

    // 部门下拉组件
    let deptTree = function (orgId) {
        if (orgId == null) {
            orgId = 0; // 后端Integer类型接收
        }
        zmnSelect.deleteXmSelect("deptId");
        var objMap = zmnSelect.select({
            'deptId': {
                isRadio: true, //单选标识：true-单选；false-多选
                selectedValues: [json.role.deptId == null ? '' : json.role.deptId], //已选中值（用于回显)
                tree: {
                    enable: true,
                    //是否展示三角图标
                    showFolderIcon: true,
                    //非严格模式
                    strict: false,
                },
                propMap: {  //选项参数映射配置
                    name: "deptName",   //显示值
                    value: "deptId"  //选中值
                },
                remote: { //远程获取下拉数据
                    enable: true, //开关：true-开启；false-关闭
                    url: '/common/dept/listDeptTree.action?orgId=' + orgId, //服务url
                },
            }
        });
        objMap['deptId'].update({
            filterable: false,
            autoRow: false,
            model: {label: {type: 'text'}},
            on: function (data) {
                if (data.isAdd) {
                    return data.change.slice(0, 1)
                }
            }
        })
    }

    // 组织架构
    if (json.isGlobalAdmin) {
        zmnSelect.selectlOrganizes(
            {
                platSelectId: 'plat', //平台ID
                orgTwoSelectId: 'orgTwoId', //大区ID
                orgThrSelectId: 'orgId', //子公司ID
            },
            {
                platSelectId: {
                    selectedValues: json.orgOneId,
                    events: {
                        changed: function (values) {}
                    }
                },
                orgTwoSelectId: {
                    selectedValues: json.orgTwoId,
                    events: {
                        changed: function (values) {}
                    }
                },
                orgThrSelectId: {
                    isRadio: true,
                    selectedValues: json.orgThrId,
                    events: {
                        changed: function (values) {}
                    }
                }
            }
        );
    }

    // 部门
    if (zmnUtil.isEmptyNull(json.roleId)) {
        deptTree();
    } else {
        if (json.orgThrId) {
            deptTree(json.orgThrId)
        } else if (json.orgTwoId) {
            deptTree(json.orgTwoId)
        } else {
            deptTree(json.orgOneId);
        }
    }

    // 自定义验证规则
    form.verify({
        exportLimit: function (value) {
            if (zmnUtil.isEmptyNull(value)) {
                return;
            }
            if (!zmnUtil.isNumber(value, false)) {
                return "请填写整数";
            }
            if (value > 10000) {
                return "导出最大值限制10000";
            }
        },
        dayLimit: function (value) {
            if (zmnUtil.isEmptyNull(value)) {
                return;
            }
            if (!zmnUtil.isNumber(value, false)) {
                return "请填写整数";
            }
            if (value > 3650) {
                return "前溯天数限制最大值3650";
            }
        },
        numberLimit: function (value) {
            if (zmnUtil.isEmptyNull(value)) {
                return;
            }
            if (!zmnUtil.isNumber(value, false)) {
                return "请填写正整数";
            }
            if (value > 50000) {
                return "查询条数最大值50000";
            }
        },
    });

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;
        let url = "/role/modify/addEdit.action";
        let jumpStop = false;
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (json) {
                let message = json.status == zmnStatus.SUCCESS ? "操作成功！" : json.message;
                layer.msg(message);
                if (json.status == zmnStatus.SUCCESS && !zmnUtil.isNotZeroInteger(data.roleId)) {
                    zmnPage.direct(zmnApp.SYS_BASE_URL + "/role/detail.action?roleId=" + json.data, 0);
                } else if (json.status == zmnStatus.FAIL) {
                    jumpStop = true;
                }
            },
            error: function (XMLHttpRequest, troleStatus, errorThrown) {
            }
        })
        if (jumpStop || data.roleId * 1 > 0) {
            return false;
        }
    });

    //对外暴露的接口
    exports('role_detail', {});
});
